<?Lassoscript
// Last modified 9/22/09 by Eric Landmann

// FUNCTIONALITY
// Include file used in response page to add an Info entry

// CHANGE NOTES
// 4/17/09
// Version 1.0
// 9/22/09
// Straightened out problem with Info Requests

// Debugging
// Var:'svDebug' = 'Y';

// Converting action_params
Var:'vError' = '';
Var:'vOption' = '';
// The action_param "Action" is passed by the frm_info.inc form to indicate whether it is an add or edit
// Action = Update, means the request was an Edit (from frm_info.inc)
// Action = Add, means the request was an Add (from frm_info.inc)
Var:'vAction' = (Action_Param:'Action');

If: $svDebug == 'Y';
	'<br>\r';
	'<strong>info_addresponse.inc</strong><br>\r';
/If;

// Checking Action to make sure it's only posting from the Add page
// ============= ADD ========
If: $vAction == 'Add';

	// Convert action_params
	Include:($svLibsPath)'info_params.inc';

	// If an error was generated from the Params, no point continuing, so bail immediately
	If: $vError == '';

		// Create name to display
		Var:'vDisplayName' = $vName;

		// Construct the query to add the CustInfo
		Var:'SQLAddContactInfo' = '
		INSERT INTO ' $svContactInfoTable '  (
			Name,
			Age,
			Address,
			City,
			State,
			Mail,
			Phone,
			Comment,
			Display,
			DateCreated
			) 
		VALUES (
			"'(Encode_SQL:(Var:'vName'))'",
			"'(Encode_SQL:(Var:'vAge'))'",
			"'(Encode_SQL:(Var:'vAddress'))'",
			"'(Encode_SQL:(Var:'vCity'))'",
			"'(Encode_SQL:(Var:'vState'))'",
			"'(Encode_SQL:(Var:'vEmail'))'",
			"'(Encode_SQL:(Var:'vPhone'))'",
			"'(Encode_SQL:(Var:'vComment'))'",
			"'(Encode_SQL:(Var:'vDisplay'))'",
			"'(Encode_SQL:(Var:'vDateCreated'))'"
			);';
	
			Debug;
				'128: SQLAddContactInfo = ' $SQLAddContactInfo '<br>\n';
			/Debug;
	
		Inline: $IV_AddContactInfo, -SQL=$SQLAddContactInfo;
			
			If:(Error_CurrentError)==(Error_NoError);
		
				// Get new record ID value from LAST_INSERT_ID() for link
				Inline: -SQL='SELECT LAST_INSERT_ID() AS NewCustomerInfoID';
					Var:'vNewCustomerInfoID' = (Field:'NewCustomerInfoID');
				/Inline;

				// If no error, Add was successful, dump out Error 2001 "Add Successful"
				Var:'vError' = '2001';
				Var:'vOption' = 'customer info';

			// There was an error
			Else;
				Var:'vError' = (Error_CurrentError);
			/If;
		
		/Inline;

	/If;

	Debug;
		'137: vError = ' $vError '<br>\n';
		'137: vOption = ' $vOption '<br>\n';
	/Debug;
// ============= EDIT ========
// Action = Update, means the request came from the Edit page
Else: $vAction == 'Update';

	// Convert action_params
	Include:($svLibsPath)'info_params.inc';

	// Copying  record ID to vNewCustomerInfoID to build link
	// This is so we can just use vNewCustomerInfoID at bottom of page for both add and edit
	Var:'vNewCustomerInfoID'=(Var:'vID');

	// If an error was generated from the Params, no point continuing, so bail immediately
	If: $vError == '';

		// Construct the query to Update the CustInfo
		// Not changing the Date_Created fields
		// NOTE: TID and SID are NOT IMPLEMENTED at this time
		Var:'SQLUpdateContactInfo' = '
		UPDATE ' $svContactInfoTable ' SET
			Name = "'(Encode_SQL:(Var:'vName'))'",
			Age = "'(Encode_SQL:(Var:'vAge'))'",
			Address = "'(Encode_SQL:(Var:'vAddress'))'",
			City = "'(Encode_SQL:(Var:'vCity'))'",
			State = "'(Encode_SQL:(Var:'vState'))'",
			Mail = "'(Encode_SQL:(Var:'vEmail'))'",
			Phone = "'(Encode_SQL:(Var:'vPhone'))'",
			Comment = "'(Encode_SQL:(Var:'vComment'))'",
			Display = "'(Encode_SQL:(Var:'vDisplay'))'",
			Topic_ID = "'(Encode_SQL:(Var:'vTID'))'",
			Section_ID = "'(Encode_SQL:(Var:'vSID'))'"
			WHERE ID = "' $vID '"';

		Debug;
			'224: SQLUpdateContactInfo = ' $SQLUpdateContactInfo '<br>\n';
		/Debug;
	
		Inline: $IV_UpdateContactInfo, -SQL=$SQLUpdateContactInfo;
			
			If:(Error_CurrentError)==(Error_NoError);
		
				// If no error, Update was successful, dump out Error 5022 "Update Successful"
				Var:'vError' = '5022';
				Var:'vOption' = 'customer info';
				Debug;
					'233: vError = ' $vError '<br>\n';
					'233: vOption = ' $vOption '<br>\n';
				/Debug;
		
			// There was an error
			Else;
				Var:'vError' = (Error_CurrentError);
			/If;
		
		/Inline;

		// vError == ''
		/If;
		
// ============= DELETE ========
Else: $vAction == 'Delete';

	// Convert action_params
	Var:'vID'=(Action_Param:'ID');
		
	// Build the Delete Query
	Var:'SQLDeleteQuery' = (string);
	Var:'SQLDeleteQuery' = 'DELETE FROM ' $svSiteDatabase'.'$svContactInfoTable ' WHERE ID = "' $vID '"';
	
	// Delete the record
	Inline: $IV_SearchContactInfo, -SQL=$SQLDeleteQuery;
		// If delete successful, output 5024 "Deletion Successful" and go ahead and delete attachments,
		If: (Error_Code) == '0';
			Var:'vError' = '5024';
			Var:'vOption' = $vID;

		Else;
			Var:'vError' = '5025';
			Var:'vOption' = (Error_CurrentError);
			Var:'URL' = ('setup_editrecord.lasso?DataType=Info&Error='$vError'&Option='$vOption);
			If: $svDebug == 'Y';
				'175: URL = ' $URL '<br>\n';
				'<a href="'$URL'">' $URL '</a><br>\n';
			Else;
				Redirect_URL: $URL;
			/If;
		/If;
	/Inline;

// Referrer not good, somebody is messing with us
// Dump out error 9002, "Database Error"
Else;
	Var:'vError' = '9002';
/If;


// Deal with the results of the processing

// ============= ADD ========
// If any other error other than 2001 "Add Successful" do a redirect
If: $vAction == 'Add';
	// Standard Error Table
	If: (Var:'vError') == '2001';
		LI_ShowError3: -ErrNum=$vError, -Option=$vOption;


// Edit New Listing Link
?>
<div align="left">
	View the record for <a href="setup_editrecord.lasso?DataType=Info&ID=[Var:'vNewCustomerInfoID']&New=Y"><strong>[$vName]</strong></a>
</div>
<?Lassoscript
	Else;
		Var:'URL' = ('setup_add.lasso?Error='$vError'&Option='$vOption'&'(Client_POSTArgs));
		If: $svDebug == 'Y';
			Debug;
				'273: URL = ' $URL '<br>';
				'<a href="'$URL'">' $URL '</a><br>\n';
			/Debug;
		Else;
			Redirect_URL: $URL;
		/If;
	/If;
/If;

// ============= EDIT ========
// If vAction = Edit, Always redirect to edit page
// If record updated OK, will get a 5022 "Update Successful" message, but do NOT pass the Params, only the ID
// This forces the system to do a new lookup
If: $vAction == 'Update';
	If: (Var:'vError') == '5022';
		Var:'URL' = ('setup_editrecord.lasso?DataType=Info&ID='$vNewCustomerInfoID'&New=Y&Error='$vError'&Option='$vOption);
	Else;
		Var:'URL' = ('setup_editrecord.lasso?New=Y&Error='$vError'&Option='$vOption'&'(Client_POSTArgs));
	/If;
	If: $svDebug == 'Y';
		Debug;
			'294: URL = ' $URL '<br>';
			'<a href="'$URL'">' $URL '</a><br>\n';
		/Debug;
	Else;
		Redirect_URL: $URL;
	/If;
/If;

// ============= DELETE ========
// If Action = Delete, redirect to Customer Info Search page
If: $vAction == 'Delete';
	Var:'URL' = ('setup_edit.lasso?Datatype=Info&Error='$vError'&Option='$vOption);
	If: $svDebug == 'Y';
		Debug;
			'239: URL = ' $URL '<br>';
			'<a href="'$URL'">' $URL '</a><br>\n';
		/Debug;
	Else;
		Redirect_URL: $URL;
	/If;
/If;
?>
